package com.wing.cabinet.utils;

import cn.hutool.core.date.DateUtil;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class FindDateStatusUtil {
    //判断选择的日期是否是本周
    public static boolean isThisWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(time);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是今天
    public static boolean isToday(Date time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    //判断选择的日期是否是本月
    public static boolean isThisMonth(Date time) {
        return isThisTime(time, "yyyy-MM");
    }

    //判断选择的日期是否是本年
    public static boolean isThisYear(Date time) {
        return isThisTime(time, "yyyy");
    }

    //判断选择的日期是否是本季度
    public static boolean isThisQuarter(Date time) {
        Date QuarterStart = getCurrentQuarterStartTime();
        Date QuarterEnd = getCurrentQuarterEndTime();
        return time.after(QuarterStart) && time.before(QuarterEnd);
    }

    private static boolean isThisTime(Date time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(time);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * 获得季度开始时间
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 3);
        return cal.getTime();
    }


    public static String getTodayTime(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     * 获取昨天
     * @return String
     * */
    public static String getYesToday(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static String getMonthStartTime(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00：00：00";
    }
    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEndTime(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }
    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStartTime(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }
    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEndTime(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    /**
     * 本季度开始时间
     * @param date
     * @return
     */
    public static String getQuarterStartTime(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        int curMonth = cDay.get(Calendar.MONTH);
        if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH) {
            cDay.set(Calendar.MONTH, Calendar.JANUARY);
        }
        if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE) {
            cDay.set(Calendar.MONTH, Calendar.APRIL);
        }
        if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
            cDay.set(Calendar.MONTH, Calendar.JULY);
        }
        if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
            cDay.set(Calendar.MONTH, Calendar.OCTOBER);
        }
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String getTime = formatter.format(cDay.getTime().getTime())+" 00:00:00";
        return getTime;
    }

    /**
     * 本季度结束时间
     * @param date
     * @return
     */
    public static String getQuarterEndTime(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        int curMonth = cDay.get(Calendar.MONTH);
        if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
            cDay.set(Calendar.MONTH, Calendar.MARCH);
        }
        if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
            cDay.set(Calendar.MONTH, Calendar.JUNE);
        }
        if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
            cDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
        }
        if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
            cDay.set(Calendar.MONTH, Calendar.DECEMBER);
        }
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");
        String getTime = formatter.format(cDay.getTime().getTime())+" 23:59:59";
        return getTime;
    }

    /**
     * 获取本年的第一天
     * @return String
     * **/
    public static String getYearStartTime() {
        return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01 00:00:00";
    }

    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEndTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
    }

    /**
     * @Description: 获取当前周第一天（周一）
     * @Author: wsp
     **/
    public static String getWeekStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取传入日期属于星期几，根据星期几进行不同处理
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        //周日：需要减去6天为周一。（当传入日期为周日时，若我们直接设置日历天数为周一，则日期会变为下一周的周一，而非当前周）
        if (dayOfWeek == 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -6);
        } else {
            //周二 至 周六：直接获取周一即可
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        }
        Date time=calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }


    public static int daysBetween(String startdate,String enddate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        int days = 0;
        try {
            calendar.setTime(sdf.parse(startdate));
            long start = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(enddate));
            long end = calendar.getTimeInMillis();
            long betweenDays=(end-start)/(1000*3600*24);
            days = Integer.parseInt(String.valueOf(betweenDays));
        }catch (Exception e){
        }
        return days;
    }

    /**
     * 取指定日期所在月的第一天
     * @param date
     * @return
     */
    public static String getFirstOfMonth(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(date));
        }catch (Exception e){
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Date time=calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }

    /**
     * 获取指定日期所在月的第最后一天
     * @param date 日期
     * @return  最后一天
     */
    public static String getLastOfMonth(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(date));
        }catch (Exception e){
            e.printStackTrace();
        }
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    /**
     * 获得上周第一天 周日 00:00:00
     */
    public static Date getWeekStartDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        calendar.add(Calendar.DATE, -6);
        return DateUtil.beginOfDay(calendar.getTime());
    }

    /**
     * 获得上周最后一天 周日 23:59:59
     */
    public static Date getWeekEndDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekStartDay());
        calendar.add(Calendar.DATE, 6);
        return DateUtil.endOfDay(calendar.getTime());
    }

    // 根据日期获取上周的开始时间
    public static Date getBeginDayOfLastWeek(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(format.parse(date));
        }catch (Exception e){
            e.printStackTrace();
        }
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    // 根据日期获取上周的结束时间
    public static Date getEndDayOfLastWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取上一天
    public static String getBeforeDay(String dateTime){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date=simpleDateFormat.parse(dateTime);
        }catch (ParseException e){
            e.getMessage();
        }
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        //往前一天
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        return simpleDateFormat.format(calendar.getTime());
    }

    // 根据日期获取上个月的开始时间
    public static String getEndDayOfLastMonth(String dateTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try{
            cal.setTime(format.parse(dateTime));
        }catch (ParseException e){
            e.getMessage();
        }
        /* 当前日期月份-1 */
        cal.add(Calendar.MONTH, -1);
        // 得到前一个月的第一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return format.format(cal.getTime())+" 00:00:00";
    }



    // 根据日期获取上个月的结束时间
    public static String getBeginEndDayOfLastMonth(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try{
            cal.setTime(format.parse(date));
        }catch (ParseException e){
            e.getMessage();
        }
        /* 当前日期月份-1 */
        cal.add(Calendar.MONTH, -1);
        /// 得到前一个月的最后一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(cal.getTime())+" 23:59:59";
    }

    /**
     * 根据日期获取本周的开始时间
     * @param dateTime
     * @return
     */
    public static String getBeginDayOfCurrentWeek(String dateTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try{
            cal.setTime(format.parse(dateTime));
        }catch (ParseException e){
            e.getMessage();
        }
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        //所在周开始日期
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())+" 00:00:00";
    }

    /**
     * 根据日期获取本周的结束时间
     * @param dateTime
     * @return
     */
    public static String getEndDayOfCurrentWeek(String dateTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try{
            cal.setTime(format.parse(dateTime));
        }catch (ParseException e){
            e.getMessage();
        }
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())+" 23:59:59";
    }

    //当前时间往前推一个月
    public static List<LocalDateTime> dateUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().plusMonths(-1), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }
    //当前时间往前推一周
    public static List<LocalDateTime> weekUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().plusWeeks(-1), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }
    //当天
    public static List<LocalDateTime> dayUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }

    //当月
    public static List<LocalDateTime> monthUtil() {//开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }

    /**
     * 获取上个月月开始时间和结束时间
     *
     * @return
     */
    public static Map getLastMonthTime() {
        Map map = new HashMap();
        try {
            Long startTime = getLastMonthStartTime();
            Long endTime = getLastMonthEndTime();
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault()));
            String endTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault()));
            map.put("startDate", startTimeStr);
            map.put("endDate", endTimeStr);
        }catch (Exception e){
        }
        return map;
    }

    public static Long getLastMonthStartTime() throws Exception {
        Long currentTime = System.currentTimeMillis();
        String timeZone = "GMT+8:00";
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(currentTime);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTimeInMillis();
    }

    public static Long getLastMonthEndTime() {
        Long currentTime = System.currentTimeMillis();

        String timeZone = "GMT+8:00";
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(currentTime);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        return calendar.getTimeInMillis();
    }

    public static String getBeforeXMonthDate(String date,int month){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //将传过来的日期设置给calendar
            calendar.setTime(sdf.parse(date));
            //System.out.println("当前日期="+sdf.format(calendar.getTime()));
            //将传过来的月份减去X个月或者加上X个月
            calendar.add(Calendar.MONTH, month);
            //System.out.println("向前推12月之前的日期="+sdf.format(calendar.getTime()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return sdf.format(calendar.getTime());
    }

}

